package com.hengtongxing.hejiayun.shop.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.bean.OrderListBean;
import com.hengtongxing.hejiayun.utils.GlideUtils;
import com.hengtongxing.hejiayun.widget.FullyGridLayoutManager;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class OrderEvaluateListAdapter extends RecyclerView.Adapter<OrderEvaluateListAdapter.ViewHolder> {


    private List<OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public OrderEvaluateListAdapter(List<OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(OrderEvaluateListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_order_comment_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean dataBean = listsBeans.get(position);
        GlideUtils.loadImage(context, holder.ivGoodsThumb, dataBean.getShop_goods_thumb());
        holder.tvGoodsName.setText(dataBean.getShop_goods_name());
        FullyGridLayoutManager fullyGridLayoutManager = new FullyGridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false);
        holder.recyclerViewPicture.setLayoutManager(fullyGridLayoutManager);
        Adapter_PictureSelector adapter = new Adapter_PictureSelector(context);
        adapter.setImgList(dataBean.getImgList());
        holder.recyclerViewPicture.setAdapter(adapter);
        adapter.setOnAddPicClickListener(new Adapter_PictureSelector.OnAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                onCallBackListener.showPictureSelect(adapter, position);
            }
        });
        adapter.setOnItemClickListener(new Adapter_PictureSelector.OnItemClickListener() {
            @Override
            public void onItemClick(int chilPposition, View v) {
                onCallBackListener.handlerAdapterItem(adapter, position, chilPposition);
            }
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.iv_goods_thumb)
        RoundedImageView ivGoodsThumb;
        @BindView(R.id.tv_goods_name)
        TextView tvGoodsName;
        @BindView(R.id.edit_goods_evaluate)
        EditText editGoodsEvaluate;
        @BindView(R.id.recyclerView_picture)
        RecyclerView recyclerViewPicture;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            editGoodsEvaluate.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    listsBeans.get(getAdapterPosition()).setComment(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }

    public interface onCallBackListener {
        void showPictureSelect(Adapter_PictureSelector adapter, int position);

        void handlerAdapterItem(Adapter_PictureSelector adapter, int position, int childPosition);
    }
}
